#------------------------------------------------------------------------------
# Project:     ballandpaddlegame (midterm)
# Name:        Ball.py
# Purpose:     Bouncing Ball Object
# 
# Authors:     Matthew Hahn
# Main Author: Matthew Hahn
#
# Created:     1/15/2013
# Copyright:   (c) Matthew Hahn 2012
# Licence:     New BSD
#------------------------------------------------------------------------------
import pygame, math, sys, random

class Ball():
    def __init__(self, speed, position, waitTime=1):
        self.surfaces = []
        self.surfaces += [pygame.image.load("rsc/ball/ball1-1.png")]
        self.surfaces += [pygame.image.load("rsc/ball/ball1-2.png")]
        self.frame = 0
        self.maxFrame = len(self.surfaces)-1
        self.surface = self.surfaces[self.frame]
        self.rect = self.surface.get_rect()
        self.radius = self.rect.width/2
        self.waitCount = 0
        self.waitTime = waitTime
        self.speed = speed
        self.place(position)
        self.living = True
        if pygame.mixer:
            self.bounceSound = pygame.mixer.Sound("bounce.wav")
        
    def  __str__(self):
        return "I'm a ball " + str(self.rect.center) + str(self.speed) + str(self.living)
     
    def place(self, position):
        print "I've moved to", position
        self.rect = self.rect.move(position)
        #Taken from ball.py in balldemo
        
    def move(self):
        print "I've moved", self.speed
        self.rect = self.rect.move(self.speed)
        #taken from robot.py in zombotapocolypse
        
        if self.waitCount < self.waitTime:
            self.waitCount += 1
        else:
            self.waitCount = 0
            if self.frame < len(self.surfaces) - 1:
                self.frame += 1
            else:
                self.frame = 0
            self.surface = self.surfaces[self.frame]
            #taken from ball.py

    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
        #taken from robot.py
        
    def collideFloor(self, screenHeight):
        print "trying to hit bottom of screen", screenHeight
        if self.living:
            if (self.rect.bottom > screenHeight):
                self.living = False
    
    def collideWall(self, screenWidth, screenHeight):
        print "trying to hit edges of screen", screenWidth, screenHeight
        if (self.rect.left < 0 
            or self.rect.right > screenWidth):
            self.speed[0] = self.speed[0]*-1
        if (self.rect.top < 0):
            self.speed[1] = self.speed[1]*-1
            #taken from ball.py
        
    def collidePaddle(self, paddle):
        print "trying to hit paddle", str(paddle)
        if self.living and paddle.living:
            if (self.rect.right > paddle.rect.left 
                and self.rect.left < paddle.rect.right):
                if (self.rect.bottom > paddle.rect.top and 
                    self.rect.top < paddle.rect.bottom):
                    if (self.distToPoint(paddle.rect.center)
                        < self.radius + paddle.radius):  
                        self.speed[0] = self.speed[0] * -1
                        self.speed[1] = self.speed[1] * -1
                        #taken from ball.py

        #if pygame.mixer:
        #   self.bounceSound.play()
        
    def collideBall(self, other):
        if self.living and other.living:
            if (self.rect.right > other.rect.left 
                and self.rect.left < other.rect.right):
                if (self.rect.bottom > other.rect.top and 
                    self.rect.top < other.rect.bottom):
                    if (self.distToPoint(other.rect.center)
                        < self.radius + other.radius):  
                        self.speed[0] = self.speed[0] * -1
                        self.speed[1] = self.speed[1] * -1
                        other.speed[0] = other.speed[0] * -1
                        other.speed[1] = other.speed[1] * -1
        print "trying to hit ", str(other)
        #taken from ball.py
        
        
        
        
        
        
        
        
        
        
        